import React, { useState, useEffect, useCallback } from 'react';
import { useHistory } from 'react-router-dom';
import { postSofPdPlanActFindHisPlan } from '../../api'
import SelectModules from './components/SelectModules'
import { HistoryPlanAll } from './style'
import { message, PageHeader, Table, Button } from 'antd'


const HistoryPlan = () => {
    const history = useHistory()
    const [listData, setListData] = useState([])
    const [total, setTotal] = useState(0)
    const [currentPage, setCurrentPage] = useState(1)
    const [size, setSize] = useState(10)
    const [selectData, setSelectData] = useState({})
    const listFun = useCallback(
        (parames = {
            currentPage: 1,
            size: 10
        }) => {
            ; (async () => {
                const { code, msg, data } = await postSofPdPlanActFindHisPlan(parames)
                if (code === '20000') {
                    setListData(data.records)
                    setTotal(data.total)
                    setCurrentPage(parames.currentPage)
                    setSize(parames.size)
                } else {
                    message.error(msg)
                }
            })()
        },
        [],
    )
    useEffect(() => {
        listFun()
    }, [listFun])
    //进入详情
    const handleEnterDetails = (id) => {
        history.push({
            pathname: '/productionPlan/details', state: {
                pdPlanId: id
            }
        })
    }
    //分页
    const pagination = {
        //是否可以改变pagesize//
        showSizeChanger: true,
        //是否可以快速跳转到某页
        showQuickJumper: true,
        //展示一共有多少条数据//
        showTotal: () => `共${total}条`,
        //每页条数
        pageSize: size,
        //当前页数
        current: currentPage,
        //数据总数
        total: total,
        //pageSize 变化的回调
        onShowSizeChange: (current, pageSize) => changePageSize(pageSize, current),
        //页码改变的回调，参数是改变后的页码及每页条数
        onChange: (current) => changePage(current),
    }
    //改变每页条数
    const changePageSize = (pageSizeFun, current) => {
        const parames = {
            currentPage: current,
            size: pageSizeFun,
            ...selectData
        }
        listFun(parames);
    }
    //改变页数
    const changePage = (current) => {
        const parames = {
            currentPage: current,
            size,
            ...selectData
        }
        listFun(parames);
    }
    const columns = [
        {
            title: '客户名称',
            dataIndex: 'clientName',
            render: (text, recode) => <Button type='link' onClick={() => handleEnterDetails(recode.id)}>{text}</Button>

        },
        {
            title: '合同编号',
            dataIndex: 'conNo'
        },
        {
            title: '产品',
            dataIndex: 'product'
        },
        {
            title: '预计发货时间',
            dataIndex: 'antEndDate'
        },
        {
            title: '预计生产时间',
            dataIndex: 'antStartDate'
        },
        {
            title: '实际发货时间',
            dataIndex: 'ptlEndDate'
        },

        {
            title: '实际生产时间',
            dataIndex: 'ptlStartDate'
        },
        {
            title: '业务员',
            dataIndex: 'sellName'
        }
    ];
    return <HistoryPlanAll>
        <PageHeader
            className="site-page-header"
            onBack={() => null}
            title="历史任务"
        />
        <SelectModules setSelectData={setSelectData} listFun={listFun} />
        <Table dataSource={listData} columns={columns} pagination={pagination} rowKey='id' />
    </HistoryPlanAll>
}

export default HistoryPlan